﻿using System;
using System.Linq;
using System.Windows.Controls;
using WindowsPhoneMVC.Navigation.Interfaces;

namespace WindowsPhoneMVC.Navigation
{
    public static class NavigatorExtensions
    {
        public static void Invoke<TController>(this INavigator navigator, string actionName, params object[] parameters) where TController : Controller
        {
            navigator.Invoke(typeof(TController).Name, actionName, parameters);
        }

        public static void InvokeBackground<TController>(this INavigator navigator, string actionName, params object[] parameters) where TController : Controller
        {
            navigator.InvokeBackground(typeof(TController).Name, actionName, parameters);
        }

        public static void InvokeWithResult<TController, TResult>(this INavigator navigator, string actionName, Action<TResult> completeCallback, params object[] parameters)
        {
            navigator.InvokeWithResult(typeof(TController).Name, actionName, completeCallback, parameters);            
        }

        public static void InvokeWithResult<TResult>(this INavigator navigator, string controllerName, string actionName, Action<TResult> completeCallback, params object[] parameters)
        {
            var navigationParameters = parameters.Select(p => new NavigationParameter(p)).ToArray();
            var navigationRequest = new NavigationRequest(navigator.NavigationFrameName, controllerName, actionName, navigationParameters);
            navigator.InvokeWithResult(navigationRequest, r=>completeCallback((TResult)r));
        }

        public static void Invoke(this INavigator navigator, string controllerName, string actionName, params object[] parameters)
        {
            var navigationParameters = parameters.Select(p => new NavigationParameter(p)).ToArray();
            var navigationRequest = new NavigationRequest(navigator.NavigationFrameName, controllerName, actionName, navigationParameters);
            navigator.Invoke(navigationRequest);
        }

        public static void InvokeBackground(this INavigator navigator, string controllerName, string actionName, params object[] parameters)
        {
            var navigationParameters = parameters.Select(p => new NavigationParameter(p)).ToArray();
            var navigationRequest = new NavigationRequest(navigator.NavigationFrameName, controllerName, actionName, navigationParameters);
            navigator.InvokeBackground(navigationRequest);
        }

        public static void InvokePartial(this INavigator navigator, Action<bool, UserControl> loadComplete, string controllerName, string actionName, params object[] parameters)
        {
            var navigationParameters = parameters.Select(p => new NavigationParameter(p)).ToArray();
            var navigationRequest = new NavigationRequest(navigator.NavigationFrameName, controllerName, actionName, navigationParameters);
            navigator.InvokePartial(navigationRequest, loadComplete);
        }
    }
}